package dk.modified.reshary;

import java.io.IOException;
import java.net.HttpURLConnection;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;

import com.squareup.picasso.Picasso;
import com.squareup.picasso.UrlConnectionDownloader;


public final class PabloPicasso {
	private static Picasso instance;

	public static Picasso with(Context context) {
		if (instance == null) {
			PackageInfo pInfo = null;
			try {
				pInfo = context.getPackageManager().getPackageInfo(
						context.getPackageName(), 0);
			} catch (NameNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			final String version = pInfo.versionName;

			Picasso.Builder builder = new Picasso.Builder(context);
			builder.downloader(new UrlConnectionDownloader(context) {
				@Override
				protected HttpURLConnection openConnection(Uri uri)
						throws IOException {
					HttpURLConnection connection = super.openConnection(uri);
					connection.setRequestProperty("User-Agent",
							"reshary.com KBJdfbkjf88jkb Agent " + version);
					return connection;
				}
			});
			return builder.build();
		}
		return instance;
	}

	private PabloPicasso() {
		throw new AssertionError("No instances.");
	}
}